const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nickname: '',
        cardID: '',
        bankName: '',
        bank_address:''

    },





    setNickname: function(e){
        this.setData({
            nickname: e.detail.value
        })
    },

    setCardID: function(e){
        this.setData({
            cardID: e.detail.value
        })
    },

    setBankName: function(e){
        this.setData({
            bankName: e.detail.value
        })
    },

    setBankAddress: function(e){
        this.setData({
            bank_address: e.detail.value
        })
    },
    

    




    addBank: function(){
        var nickname = this.data.nickname, cardID = this.data.cardID, bankName = this.data.bankName, bank_address = this.data.bank_address;
        if(!nickname) {
            wx.showToast({
              title: '真实姓名不能为空',
              icon: 'none'
            });
            return false;
        }

        if(!cardID) {
            wx.showToast({
              title: '银行卡号不能为空',
              icon: 'none'
            });
            return false;
        }

        if(!bankName) {
            wx.showToast({
              title: '银行名称不能为空',
              icon: 'none'
            });
            return false;
        }
        app.ajax({
            url: '/api/user/addbank',
            data: {
                nickname: nickname,
                cardID: cardID,
                bank_name: bankName,
                bank_address:bank_address
            },
            success: res => {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                });

               
                wx.navigateTo({
                    url: '/pages/bank_list/bank_list'
                  })
              
            }
        }, 1);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var date_arr = [];
        for (var i = 0; i < 3; i++) {
            var now = new Date().getTime() + i * 86400000;
            date_arr.push(this.getDate(now));
        }
        console.log(date_arr);
        var start_date = this.getDate(Date.now() + 86400000 * 3),
            end_date = this.getDate(Date.now() + 86400000 * 30);
        this.setData({
            date_arr: date_arr,
            date_start: start_date.date,
            date_end: end_date.date,
            select_date: date_arr[0].date
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.userinfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    userinfo: function () {
        app.ajax({
            url: '/api/user/userinfo',
            success: res => {
                if (res.code == 1) {
                    wx.setStorageSync('user', res.data);
                    
                    var is_vip = false
                    if(res.data.vip == 0)   is_vip = false
                    else  is_vip = true
                    this.setData({
                        head: res.data.avatar,
                        name: res.data.nickname,
                        mobile: res.data.mobile,
                        sex:res.data.sex,
                        birthday: res.data.birthday,
                        vip_name: res.data.vip_name,
                        vip_card_number: res.data.vip_name,
                        msg: res.data.personal_signature
                    })
                }
            }
        })
    },
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        var date = e.detail.value;
        this.setData({
            birthday: date,
            select_date: date
        })
    },
    getDate: function (now) {
        var date = new Date(now),
            y = date.getFullYear(),
            m = date.getMonth() + 1,
            w = date.getDay(),
            d = date.getDate(),
            weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
        m = ('0' + m).slice(-2);
        d = ('0' + d).slice(-2);
        return {
            week: weeks[w],
            date: y + '-' + m + '-' + d,
            name: m + '月' + d + '日'
        };
    }
})